home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / misc / gms_dev.lha / GMSDev / Source / C / Misc / SetField.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-15  |  5.2 KB  |  208 lines

  1. /* Dice: 1> dcc -l0 -mD dpk.o tags.o SetField.c -o SetField
  2. **
  3. ** This demo tests the SetField() and GetField() functions.
  4. */
  5.  
  6. #include <proto/dpkernel.h>
  7. #include <system/sysobject.h>
  8.  
  9. BYTE *ProgName      = "SetField";
  10. BYTE *ProgAuthor    = "Paul Manias";
  11. BYTE *ProgDate      = "August 1998";
  12. BYTE *ProgCopyright = "DreamWorld Productions (c) 1998.  Freely distributable.";
  13. BYTE *ProgShort     = "Field Orientation demo.";
  14.  
  15. struct GScreen  *screen;
  16. struct Restore  *restore;
  17. struct Bob      *Rabbit;
  18. struct JoyData  *joydata;
  19. struct Picture  *bobpic;
  20. struct FileName bobfile  = { ID_FILENAME, "GMS:demos/data/Rabbit.iff" };
  21.  
  22. WORD RabbitFrames[] = {
  23.    /* Right 0 - 7 */
  24.    0,0, 8,0, 16,0, 24,0, 32,0, 40,0, 48,0, 56,0,
  25.  
  26.    /* Left 8 - 15 */
  27.    120,0, 112,0, 104,0, 96,0, 88,0, 80,0, 72,0, 64,0,
  28.  
  29.    /* Jumping 16 - 24 */
  30.    128,0, 136,0, 144,0, 152,0, 160,0, 168,0, 176,0, 184,0, 192,0,
  31.  
  32.    /* Backward Flip 25 - 47 */
  33.    0,16,     8,16,  16,16,  24,16,  32,16,  40,16,  48,16,  56,16,
  34.    64,16,   72,16,  80,16,  88,16,  96,16, 104,16, 112,16, 120,16,
  35.    128,16, 136,16, 144,16, 152,16, 160,16, 168,16, 176,16,
  36.    -1,-1
  37. };
  38.  
  39. void Demo(void);
  40. void Wrap(struct Bob *);
  41.  
  42. /***************************************************************************/
  43.  
  44. void main(void) {
  45.   LONG *palette;
  46.  
  47.   if (bobpic = Get(ID_PICTURE)) {
  48.      SetField(bobpic,FID_Source,(LONG)&bobfile);
  49.      SetField(bobpic->Bitmap,FID_MemType,MEM_BLIT);
  50.  
  51.    if (Init(bobpic,NULL)) {
  52.  
  53.     if (screen = Get(ID_SCREEN)) {
  54.      if (palette = CloneMemBlock(bobpic->Bitmap->Palette,MEM_DATA)) {
  55.         SetField(screen->Bitmap,FID_Palette,(LONG)palette);
  56.  
  57.       if (Init(screen,NULL)) {
  58.  
  59.        if (restore = InitTags(screen,
  60.             TAGS_RESTORE, NULL,
  61.             RSA_Entries,  1,
  62.             TAGEND)) {
  63.  
  64.         if (Rabbit = InitTags(screen,
  65.             TAGS_BOB,      NULL,
  66.             BBA_GfxCoords, RabbitFrames,
  67.             BBA_Width,     8,
  68.             BBA_Height,    16,
  69.             BBA_XCoord,    screen->Width/2,
  70.             BBA_YCoord,    screen->Height/2,
  71.             BBA_Attrib,    BBF_RESTORE|BBF_GENMASKS|BBF_CLIP,
  72.             BBA_SrcBitmap, bobpic->Bitmap,
  73.             TAGEND)) {
  74.  
  75.          if (joydata = Get(ID_JOYDATA)) {
  76.           if (SetField(joydata,FID_Port,2) IS ERR_OK) {  /* Forces joystick control */
  77.  
  78.            if (Init(joydata, NULL)) {
  79.               Display(screen); 
  80.               Demo();
  81.            }
  82.           }
  83.          }
  84.         }
  85.        }
  86.       }
  87.      }
  88.     }
  89.    }
  90.   }
  91.  
  92.   FreeMemBlock(palette);
  93.   Free(joydata);
  94.   Free(Rabbit);
  95.   Free(restore);
  96.   Free(screen);
  97.   Free(bobpic);
  98. }
  99.  
  100. /***************************************************************************/
  101.  
  102. void Demo(void)
  103. {
  104.   WORD AnimSpeed = 0;
  105.   WORD Direction = 0;
  106.   WORD Flip = FALSE;
  107.  
  108.   #define MAXSPEED 3
  109.  
  110.   do
  111.   {
  112.     Activate(restore); 
  113.     Draw(Rabbit);
  114.     WaitAVBL();
  115.     SwapBuffers(screen);
  116.  
  117.     /* Animate the Rabbit's movements */
  118.  
  119.     AnimSpeed++;
  120.  
  121.     if (AnimSpeed > MAXSPEED) {
  122.        AnimSpeed = 0;
  123.        Rabbit->Frame++;
  124.  
  125.        if (Direction IS 1) {
  126.           if (Rabbit->Frame < 0) Rabbit->Frame = 0;
  127.           if (Rabbit->Frame > 7) Rabbit->Frame = 0;
  128.        }
  129.        else if (Direction IS -1) {
  130.           if (Rabbit->Frame < 8) Rabbit->Frame  = 8;
  131.           if (Rabbit->Frame > 15) Rabbit->Frame = 8;
  132.        }
  133.        else {
  134.           if (Flip IS FALSE) {
  135.              if (Rabbit->Frame < 16) Rabbit->Frame = 16;
  136.              if (Rabbit->Frame > 24) Rabbit->Frame = 16;
  137.           }
  138.           else {
  139.              if (Rabbit->Frame < 25) Rabbit->Frame = 25;
  140.              if (Rabbit->Frame > 47) Rabbit->Frame = 25;
  141.           }
  142.        }
  143.     }
  144.  
  145.     /* Get the user input */
  146.  
  147.     Query(joydata);
  148.     if (Direction != NULL) {
  149.        Rabbit->XCoord += joydata->XChange;
  150.     }
  151.  
  152.     if (joydata->YChange < 0) {
  153.        if ((Direction != NULL) OR (Flip IS TRUE)) {
  154.           Rabbit->Frame = 16;
  155.           Direction = 0;
  156.           Flip = FALSE;
  157.        }
  158.     }
  159.     else if (joydata->YChange > 0) {
  160.        if ((Direction != NULL) OR (Flip IS FALSE)) {
  161.           Rabbit->Frame = 25;
  162.           Direction = 0;
  163.           Flip = TRUE;
  164.        }
  165.     }
  166.     else if (joydata->XChange > 0) {
  167.        if (Direction IS -1) {   /* If rabbit is facing left, spin right */
  168.           Rabbit->Frame -= 8;
  169.           Direction     = 1;
  170.        }
  171.        else if (Direction IS NULL) {
  172.           Rabbit->Frame = 0;
  173.           Direction     = 1;
  174.        }
  175.     }
  176.     else if (joydata->XChange < 0) {
  177.        if (Direction IS 1) {    /* If rabbit is facing right, spin left */
  178.           Rabbit->Frame += 8;
  179.           Direction     = -1;
  180.        }
  181.        else if (Direction IS NULL) {
  182.           Rabbit->Frame = 8;
  183.           Direction     = -1;
  184.        }
  185.     }
  186.  
  187.     Wrap(Rabbit);
  188.  
  189.   } while (!(GetField(joydata,FID_Buttons) & JD_FIRE1));
  190. }
  191.  
  192. /*****************************************************************************
  193. ** Function: This function will wrap a bob to the other side of a screen if
  194. **           it leaves the bob's screen borders.
  195. **
  196. ** Synopsis: Wrap(Bob);
  197. */
  198.  
  199. void Wrap(struct Bob *bob)
  200. {
  201.   if (bob->XCoord < -bob->Width)  bob->XCoord = bob->DestBitmap->Width;
  202.   if (bob->YCoord < -bob->Height) bob->YCoord = bob->DestBitmap->Height;
  203.  
  204.   if (bob->XCoord > bob->DestBitmap->Width)  bob->XCoord = -bob->Width;
  205.   if (bob->YCoord > bob->DestBitmap->Height) bob->YCoord = -bob->Height;
  206. }
  207.  
  208.